package api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import appmodel.JsonModel;
import service.SettingService;

@Controller
@RequestMapping("apiSetting")
public class SettingApiController {
	@Autowired
	private SettingService settingService;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	@ResponseBody
	public JSONObject list(HttpServletRequest request, HttpServletResponse response, JsonModel jsonModel,
			@RequestParam(value = "type", required = false) String type,
			@RequestParam(value = "cfield", required = false) String cfield) {

	      response.addHeader("Access-Control-Allow-Origin", "*");
          response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
          response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type");
          response.addHeader("Access-Control-Max-Age", "1800");//30 min
		
		Map map = new HashMap();
		map.put("type", type);
		map.put("cfield", cfield);
		List list = settingService.selectAllByType(map);

		if (list.size() > 0) {
			jsonModel.setSuccess(true);
			jsonModel.setMsg("查询成功");
			jsonModel.setjData(list);
			jsonModel.setToken("");
		} else {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("查询失败");
			jsonModel.setjData("");
			jsonModel.setToken("");
		}

		return JSONObject.fromObject(jsonModel);
	}
}
